<?php
class Model_TableAssociation implements Model_ITable
{
	private static $con = null;
	private static $reqSelectAll = null;
        private static $reqSelectAllLimit = null;
	private static $reqSelectOne = null;
        private static $reqSelectOneByLogin = null;
	private static $reqInsert = null;
	private static $reqDeleteOne = null;
	private static $reqUpdate = null;
        private static $reqCount = null;

	
	public static function setConnexion(MyPdo $pdo)
	{
		self::$con = $pdo;
	}
	
	public static function getConnexion()
	{
		return self::$con;
	}

	public static function init()
	{
		self::$reqSelectAll = self::$con->prepare('SELECT * FROM association ORDER BY id_association');
		self::$reqSelectOne = self::$con->prepare('SELECT * FROM association WHERE id_association = :idcond');
                self::$reqSelectOneByLogin = self::$con->prepare('SELECT * FROM association WHERE login = :login');
		self::$reqDeleteOne = self::$con->prepare('DELETE FROM association WHERE id_association = :idcond');
		self::$reqInsert = self::$con->prepare('INSERT INTO association(nom, description, telephone, fax, email, responsable, login, password, id_adresse) VALUES(:nom, :description, :telephone, :fax, :email, :responsable, :login, :password, :idadresse)');
		self::$reqUpdate = self::$con->prepare('UPDATE association SET nom = :nom, 
			description = :description,
			telephone = :telephone,
			fax = :fax,
			email = :email,
			responsable = :responsable,
                        login = :login,
                        password = :password,
			id_adresse = :idadresse WHERE id_association = :idcond
		');
                self::$reqCount = self::$con->prepare('SELECT COUNT(*) FROM association');
                self::$reqSelectAllLimit = self::$con->prepare('SELECT * FROM association ORDER BY id_association LIMIT :begin, :nb');
	}

	public static function fetchAll()
	{
		self::$reqSelectAll->execute();
		$results = self::$reqSelectAll->fetchAll();
		return $results;
	}

        public static function fetchAllLimit($begin, $nb)
        {
            self::$reqSelectAllLimit->bindParam(':begin', $begin, PDO::PARAM_INT);
            self::$reqSelectAllLimit->bindParam(':nb', $nb, PDO::PARAM_INT);
            self::$reqSelectAllLimit->execute();
            return self::$reqSelectAllLimit->fetchAll();
        }

	public static function fetchOne($id)
	{
		self::$reqSelectOne->execute(array(':idcond' => $id));
		$results = self::$reqSelectOne->fetchAll();
		if( empty($results[0]) )
			return array();
		else
			return $results[0];
	}

        public static function fetchOneByLogin($login)
        {
                self::$reqSelectOneByLogin->execute(array(':login' => $login));
		$results = self::$reqSelectOneByLogin->fetchAll();
		if( empty($results[0]) )
			return array();
		else
			return $results[0];
        }
	
	public static function delete($id)
	{
		self::$reqDeleteOne->execute(array(':idcond' => $id));
	}
	
	public static function insert($nom, $description, $telephone, $fax, $email, $responsable, $login, $password, $idadresse)
	{
		self::$reqInsert->execute(array(
			':nom' => $nom,
			':description' => $description,
			':telephone' => $telephone,
			':fax' => $fax,
			':email' => $email,
			':responsable' => $responsable,
                        ':login' => $login,
                        ':password' => $password,
			':idadresse' => $idadresse
		));
		return self::$con->lastInsertId();
	}
	
	public static function update($id, $nom, $description, $telephone, $fax, $email, $responsable, $login, $password, $idadresse)
	{
		self::$reqUpdate->execute(array(
			':nom' => $nom,
			':description' => $description,
			':telephone' => $telephone,
			':fax' => $fax,
			':email' => $email,
			':responsable' => $responsable,
                        ':login' => $login,
                        ':password' => $password,
			':idadresse' => $idadresse,
			':idcond' => $id
		));
	}

        public static function count()
        {
            self::$reqCount->execute();
            return self::$reqCount->fetchColumn();
        }

}
